var GH = {
    //动态加载JS,CSS
    load : function( type, id, url, callback ) 
    {
      
      var obj = document.getElementById( id );
      try {
        obj.parentNode.removeChild( obj )
      } catch(e) {
        
      }

      var insertEle = null;
      if( !type || type == "js" ) {
        insertEle = document.createElement("script");
        insertEle.setAttribute("type", "text/javascript");
        insertEle.setAttribute("src", url);
      } else if ( type == "css" ) {
        insertEle = document.createElement("link");
        insertEle.setAttribute("rel", "stylesheet");
        insertEle.setAttribute("href", url);
      }
      
      insertEle.setAttribute("id", id);
      insertEle.setAttribute("charset", "utf-8");
      
      document.getElementsByTagName("head")[0].appendChild( insertEle );
      insertEle.onload = function() {
        callback && callback();
      }
    },
    /*
     * 设置获取css样式
     * 1.css ( obj, "width" )
     * 2.css ( obj, "display", "none" )
     * 3.css ( obj, {"display" :block, "opacity": 1} )
     */
    css: function ( obj, attr, value ) 
    {
      switch( arguments.length ) {
        case 2: 
          if ( typeof arguments[1] === "string" ) {
            return getComputedStyle ( obj, false ) [ attr ]; //1
          } else {
            for ( var i in arguments[1] ) { obj.style[i] = attr[i]; } //3
          }
          break;
        case 3:
          obj.style[attr] = value; //2
          break;
        default:
          console.log( "参数传入错误" );
          break;
      }
    },
    //手机上弹出提示信息
    showMessage: function ( message ) 
    {
      var oMsg = document.createElement( "span" );
      oMsg.innerHTML = message;
      GH.css( oMsg, {
       "position": "fixed", "top": "50%", "left": "50%", "WebkitTransform": "translate(-50%,-50%)", "msTransform": "translate(-50%,-50%)", "zIndex": 9999,
       "backgroundColor": "rgba(51,51,51,.9)", "color": "#fff", "padding": ".8em 1em", 
       "border-radius": "2px", "boxShadow": "0 0 2px 2px rgba(0,0,0,0.3)",
       "transition": "all 0.5s ease-in-out",
       "WebkitTransition": "all 0.5s ease-in-out",
       "MsTransition": "all 0.5s ease-in-out",
      });
      document.body.appendChild( oMsg );
      setTimeout( function(){ 
        GH.css( oMsg, {"opacity": 0} );
        setTimeout( function(){ document.body.removeChild( oMsg ) }, 500);
      }, 2000 )
    },
    //代理事件
    delegate: function ( type, obj, target, callback ) 
    {
      obj.addEventListener( type, fun, false );
      function fun ( e ) {
        var t = e.target;
        while ( t ) {
          if ( t.nodeName.toLowerCase() === target.toLowerCase() ) {
            callback && callback.apply( t );
          }
          t = t.parentNode;
        }
      }
    },
    //json对象转换成数组
    toArray: function (data, level, path, result)
    {
      for(var o in data) {
        var p = path + (level? "["+o+"]" : o);
        if (Object.prototype.toString.call(data[o]) === '[object Array]' || Object.prototype.toString.call(data[o]) === '[object Object]') {
          this.toArray(data[o],1 + level, p, result);
        } else {
            Object.prototype.toString.call(result) === '[object Object]' && (result[p] = data[o]);
            Object.prototype.toString.call(result) === '[object Array]' && result.push( p + "=" + data[o] );
          }
      }
      return result;
    },
    ajax: function( url, json, fnSucc, type ) 
    {
      var oAjax;
      type = type ? type : "post";
      //创建ajax对象
      oAjax = new XMLHttpRequest();
      //发送的数据
      /*
      var data = "";
      for(var i in json)
      {
        data += i + "=" + json[i] + "&";
      }
      data = data.replace(/&$/,"");
      */
      var data = GH.toArray( json, 0, "", [] ).join("&");
      //GET或者POST
      if(type == "get") {
        oAjax.open("GET", url+"?"+data+new Date().getTime(), true);
        oAjax.send();
      } else if (type == "post") {
        oAjax.open("POST", url, true);
        oAjax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        oAjax.send(data);
      }
      
      oAjax.onreadystatechange = function() {
        if( oAjax.readyState == 4 && oAjax.status == 200 ) {
          try {
            //返回的json字符串转换为对象
            fnSucc( JSON.parse(oAjax.responseText) );
          } catch(e) {
            fnSucc( oAjax.responseText );
          };
        }
      }
    },
    serialize: function( form ) 
    {
      form = (typeof form === "string") ? document.getElementById( form ) : form;
    
      var input = form.getElementsByTagName( "input" ),
          select = form.getElementsByTagName( "select" ),
          textarea = form.getElementsByTagName( "textarea" );
          checkedBox = "",
          res = {}; //存放结果数据
    
      //遍历input类型  
      for(var i=0, inputSelf; inputSelf=input[i]; i++) {
      
        var inputType = inputSelf.getAttribute("type");
        switch ( inputType ) 
        {
          case "checkbox":
          {
            if ( inputSelf.checked ) { checkedBox += (inputSelf.value+",");}
            res[ inputSelf.getAttribute("name") ] = checkedBox;
          }
          case "radio":
          {
            if( inputSelf.checked ) { res[ inputSelf.getAttribute("name") ] = inputSelf.value; }
            break;
          }
          case "submit":
            break;
          default:
          {
            res[ inputSelf.getAttribute("name") ] = inputSelf.value;
            break;
          }
        }
      };
      //遍历下拉列表
      for ( var i=0, selectSelf; selectSelf=select[i]; i++ ) {
        res[ selectSelf.getAttribute("name") ] = selectSelf.value;
      };
      //遍历多行文本框
      for ( var i=0, textareaSelf; textareaSelf=textarea[i]; i++ ) {
    	  res[ textareaSelf.getAttribute("name") ] = textareaSelf.value;
      };
      return res;
    },
    pageBack: function() 
    {
      var a = window.location.href;
      if (/#top/.test(a)) {
        window.history.go(-2);
      } else {
        window.history.back();
      }
    }
}